var tree;
function deptTree(){
	tree = new dhtmlXTreeObject("tree","100%","100%",-1);
	tree.setImagePath("../imgs/");
	tree.loadXML("dept/tree.xml");
    tree.attachEvent("onClick","selectNode");
    //tree.enableTreeImages(0);
}
function selectNode(){
	var url = getAction()+"?cmd=list&formMap.type=USERS&po.deptId="+tree.getSelectedItemId();
	new Ajax.Request (url, { method: "post", onSuccess: function(transport) {
    	$("dataList").innerHTML = transport.responseText.strip();
  	}});
}

function grantRoles(){
	var selectUsers = document.all("formMap.userIds");
	for(var i=0; i<selectUsers.length; ++i){
		selectUsers.options[i].selected = true; 
	}
	$("grantForm").request({onSuccess:function(transport){
		var json = transport.responseText.strip().evalJSON();
    	alert(json.msg);
    	if(json.processId=='1')
    		window.close();
  	}});
}

function addUser(){
	var ids = document.all("formMap.ids");
	if(ids!=null){
		for(var i=0; i<ids.length; ++i){
			if(ids[i].checked && !userExist(ids[i].value)){
				var selectUsers = document.all("formMap.userIds");
				var node = ids[i].parentNode.parentNode;
				var text = node.cells[1].innerHTML+'('+node.cells[2].innerHTML+')';
				selectUsers.options[selectUsers.options.length]=new Option(text,ids[i].value);
			}
		}
	}
}

function userExist(userId){
	var selectUsers = document.all("formMap.userIds");
	var ops = selectUsers.options;
	for(var i=0; i<ops.length; ++i){
		if(ops[i].value==userId)
			return true;
	}
	return false;
}

function delUser(){
	var selectUsers = document.all("formMap.userIds");
	delOption(selectUsers);
}

function delOption(selectTag){ 
	if(selectTag.selectedIndex>=0){ 
		selectTag.remove(selectTag.selectedIndex); 
		delOption(selectTag); 
   } 
} 

function getAction(){
	return "usersRoles.action";
}